3. SpringBoot使用jms聆聽MQ


此次主要介紹jmsTemplate 主動式聆聽MQ

目標:可以透過jmsTemplate聆聽MQ(接收)

前情提要:
銜接上1篇文章分析1下
之前介紹的主要是 被動式透過API觸發
而馬上要介紹的,當MQ接收到Message 此專案服務會接收下來進行處理

那現在開始介紹jmsTemplate 的Linstener

必須知識:

  • java 8
  • spring系列
  • MVC 分層抽離
  • mq的基本知識
  • 可能需要會通靈,解釋性資訊不多XD

spring boot核心應用為以下

  1. spring starter
  2. mq spring starter
    <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.7.11</version>
     <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependency>
     <groupId>com.ibm.mq</groupId>
     <artifactId>mq-jms-spring-boot-starter</artifactId>
     <version>2.0.0</version>
    </dependency>
    

聆聽QUEUE設定檔
JmsListener.class

@Slf4j
@EnableJms // 上一篇為Application.class 使用到的, 因現在分層抽離分工好
@Component
public class JmsListener {

@Bean
public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
                                                DefaultJmsListenerContainerFactoryConfigurer configurer) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    configurer.configure(factory, connectionFactory);
    return factory;
}
}

建立一個要聆聽的MQ進入點
EntryPoint.class

@Slf4j
@Component
public class EntryPoint {

    @Autowired
    JmsMessageService jmsMessageService;

    @JmsListener(destination = "自定義佇列", containerFactory = "myFactory")
    public void receiveMessage(Message message) throws JMSException {
        log.info(jmsMessageService.getMsg(message));
    }
}

補充:

需要有mq服務 建議可以使用containter方便又快速

主要是給自己的一個紀錄,也分享給有需要的夥伴
註解部分有提及一些參考的連結,有興趣可以點進去看看喔

這是一個心血來潮,產生的文章
若有喜歡或交流的部分都歡迎在下方留言,多多關照。

#mq #jmstemplate #listener







你可能感興趣的文章

筆記、SQL 語法

筆記、SQL 語法

.Net MVC authorization Controller and Workcontext extension in razor view

.Net MVC authorization Controller and Workcontext extension in razor view

Ceres 函式庫簡介

Ceres 函式庫簡介






留言討論